package hongcaosp.app.android;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.tencent.liteav.demo.common.utils.TCConstants;
import com.tencent.rtmp.TXLiveConstants;
import com.tencent.ugc.TXRecordCommon;

import hongcaosp.app.android.attention.FollowFragment;
import hongcaosp.app.android.contact.MessageFragment;
import hongcaosp.app.android.login.RegisterActivity;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.redpacket.RedPacketService;
import hongcaosp.app.android.user.UserFragment;
import hongcaosp.app.android.user.settings.safety.bind.BindPhoneActivity;
import hongcaosp.app.android.video.MainFragment;
import hongcaosp.app.android.video.record.VideoRecordActivity;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.utils.ScreenUtils;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.view.SwitchWidget;

public class MainActivity extends BaseActivity {
    private SwitchWidget tabsWidget;
    private BaseFragment currentFragment;
    private BaseFragment[] fragments = new BaseFragment[5];

    private boolean showFull;

    private int currentIndex = 0;

    private boolean isRedStart = false;

    @Override
    protected void onResume() {
        super.onResume();
        if (!UserToken.getDefault().isLogged()) {
            tabsWidget.setItemSelected(0);
            showSelectPage(0);
        } else {
            //登录了，校验是否绑mobile
            if (TextUtils.isEmpty(UserToken.getDefault().getUser().getMobile())) {
                //需要绑定手机号
                Intent intent = new Intent(this,BindPhoneActivity.class);
                intent.putExtra("src","main");
                startActivity(intent);
                return;
            }
            if (RedPacketService.redSocket != null) return;
            // 开启抢红包链接
            Intent intent = new Intent(this,RedPacketService.class);
            String url = "ws://www.hongcaosp.com/shortvideo/webSocket/chat/ROOMNAMEBASE-rp-";
//            String url = "ws://shortvideo.udreamtech.com/shortvideo/webSocket/chat/ROOMNAMEBASE-rp-";
            intent.putExtra("url",url
                    + UserToken.getDefault().getUser().getId());
            startService(intent);
        }
        if (currentFragment != null) {
            currentFragment.onShow();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (currentFragment != null) {
            currentFragment.onHide();
        }
    }

    @Override
    protected void initData(Intent intent) {
        setDefaultFocusFragment(intent);
    }

    @Override
    protected void initView() {

        StatusBarUtil.setStatusBarDarkTheme(this, false);

        showFull = ScreenUtils.getScreenHeight(this) * 9  <= 16 * ScreenUtils.getScreenWidth(this);

        tabsWidget = findViewById(R.id.switch_tabs);
        tabsWidget.setOnSwitchedListener(new SwitchWidget.OnSwitchedListener() {
            @Override
            public boolean onItemSelected(int index) {
                if (!checkLogin()) {
                    RegisterActivity.toRegister(MainActivity.this);
                    return false;
                }
                if (index == 2) {
                    //短视频录制
                    launchRecord();
                    return false;
                } else {
                    showSelectPage(index);
                    return true;
                }
            }
        });
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    private void setDefaultFocusFragment(Intent intent) {
        int index = intent.getIntExtra("index", currentIndex);
        tabsWidget.setItemSelected(index);
        showSelectPage(index);
    }

    private boolean checkLogin() {
//        return true;
        return UserToken.getDefault().isLogged();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void launchRecord() {
        Intent intent = new Intent(this, VideoRecordActivity.class);
        intent.putExtra(TCConstants.RECORD_CONFIG_MIN_DURATION, 5 * 1000);
        if (UserToken.getDefault().getUser() != null && UserToken.getDefault().getUser().isVip()) {
            intent.putExtra(TCConstants.RECORD_CONFIG_MAX_DURATION, 60 * 1000);
        } else {
            intent.putExtra(TCConstants.RECORD_CONFIG_MAX_DURATION, 15 * 1000);
        }
        intent.putExtra(TCConstants.RECORD_CONFIG_ASPECT_RATIO, TXRecordCommon.VIDEO_ASPECT_RATIO_9_16);
        intent.putExtra(TCConstants.RECORD_CONFIG_RECOMMEND_QUALITY, TXRecordCommon.VIDEO_QUALITY_HIGH);
        intent.putExtra(TCConstants.RECORD_CONFIG_HOME_ORIENTATION, TXLiveConstants.VIDEO_ANGLE_HOME_DOWN); // 竖屏录制
        intent.putExtra(TCConstants.RECORD_CONFIG_TOUCH_FOCUS, true);
        intent.putExtra(TCConstants.RECORD_CONFIG_NEED_EDITER, false);
        startActivity(intent);
    }

    /**
     * 显示当前选中的页面
     *
     * @param index
     */
    private void showSelectPage(int index) {
        if (currentIndex == index && currentFragment != null) return;
        if (index < 1 && showFull) {
            tabsWidget.setBackgroundResource(R.color.transparent);
        } else {
            tabsWidget.setBackgroundResource(R.drawable.tab_bg);
        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        BaseFragment fragment = fragments[index];
        if (fragment == null) {
            switch (index) {
                case 0:
                    fragment = new MainFragment();//首页
                    break;
                case 1:
                    fragment = new FollowFragment();//关注
                    break;
                case 3:
                    fragment = new MessageFragment();//消息
                    break;
                case 4:
                    fragment = new UserFragment();//我
                    break;
            }
            fragments[index] = fragment;
            transaction.add(R.id.content, fragment, "tab_" + index);
        }

        if (fragment != null) {
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            currentFragment = fragment;
            transaction.show(currentFragment);
            transaction.commit();
            currentIndex = index;
        }
    }

    public static void toMainVideoPage(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra("index", 0);
        context.startActivity(intent);
    }
}
